<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="<%=request.getContextPath()%>/css/base.css" rel="stylesheet" type="text/css" />
<link href="<%=request.getContextPath()%>/css/admin.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/base.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/admin.js"></script>
</head>


<BODY class="list goodsCategory">
<DIV class=body>
<FORM id=listForm method=post action=typelist.do>
<DIV class=listBar><INPUT hideFocus class=formButton onclick="location.href='typeadd.do'" value=添加分类 type=button> </DIV>
<TABLE id=listTable class=listTable>
<TBODY>
<TR>
<TH><SPAN>分类名称</SPAN> </TH>
<TH><SPAN>排序</SPAN> </TH>
<TH><SPAN>操作</SPAN> </TH></TR>
<c:forEach items="${goodstypes}" var="goodtype">
<TR  grade="0" >
<TD class=goodsCategoryName ><SPAN id='${goodtype.id}' style="MARGIN-LEFT: 0px" class="pointer firstCategory">${goodtype.typeName} </SPAN></TD>
<TD>${goodtype.sort}</TD>
<TD><A title=查看 href="#" target=_blank>[查看]</A> <SPAN title=无法删除>[删除]</SPAN> <A title=编辑 href="typeedit.do?id=${goodtype.id}">[编辑]</A> </TD>
</TR>
</c:forEach>

</TBODY></TABLE></FORM>
</DIV>
</BODY>
<script type="text/javascript"><!--
  $().ready( function() {
  
    var $toptype= $(".pointer");
    $toptype.live('click', function() {
        var item=$(this).parent().parent();
        if(item.attr('grade')<2)//一级和二级目录展开
        {
        //如果没有下级目录，从服务器取数据
         if(item.next().html()==null || item.next().attr('grade')<=item.attr('grade'))
       	 {
			var typeid=$(this).attr('id');
			var url='<%=request.getContextPath()%>/admin/sublist.do?id='+typeid;
	    	$.get(url, function(result){	
	     	 var types=eval('('+result+')');	
	    	 additem(item,types)
	    	 });
	     }else
	     {
	       var sub=item.next();
	       var status=sub.is(":hidden");
	       while(sub.attr('grade')>item.attr('grade'))
	       {
	         if(status && (sub.attr('grade')*1-1)==item.attr('grade'))
	         {
	           sub.show();
	         }else
	         {
	           sub.hide();
	         }
	        sub=sub.next();
	       }	   
	     }
	   }
	});
  
  });
  function additem(item,subtypes)
  {
    for(i=subtypes.length-1;i>=0;i--)
    {
     var html='';
      if(item.attr('grade')==0)
      {
        html='<TR grade="1">'
               +'<TD class=goodsCategoryName ><SPAN id='+subtypes[i].id
               +' style="MARGIN-LEFT: 20px" class="pointer category">'+subtypes[i].typeName+' </SPAN></TD>'
               +'<TD>'+subtypes[i].sort+'</TD>'
               +'<TD><A title=查看 href="/shop/goods_list/health_appliances.htm" target=_blank>[查看]</A> <A class=deleteGoodsCategory title=删除 href="javascript:del('+subtypes[i].id+')" >[删除]</A> <A title=编辑 href="typeedit.do?id='+subtypes[i].id+'">[编辑]</A> </TD>'
               +'</TR>';     
      }else if(item.attr('grade')==1)
      {
        html='<TR grade="2">'
               +'<TD class=goodsCategoryName ><SPAN id='+subtypes[i].id
               +' style="MARGIN-LEFT: 40px">'+subtypes[i].typeName+' </SPAN></TD>'
               +'<TD>'+subtypes[i].sort+'</TD>'
               +'<TD><A title=查看 href="/shop/goods_list/health_appliances.htm" target=_blank>[查看]</A> <A class=deleteGoodsCategory title=删除 href="javascript:del('+subtypes[i].id+')" >[删除]</A> <A title=编辑 href="typeedit.do?id='+subtypes[i].id+'">[编辑]</A> </TD>'
               +'</TR>';     
      
      }
     
      item.after(html);
    
    }
    
  
  }
  
  function del(typeid)
  {
    $.dialog({type: "warn", content: "您确定要删除吗?", ok: "确 定", cancel: "取 消", modal: true, okCallback: deleteType});
   function deleteType(){
   var url='<%=request.getContextPath()%>/admin/typeremove.do?id='+typeid;
   $.get(url, function(result){	
        if(result==1)
        {
          $.dialog({type: "success", content: "商品类别删除成功！", modal: true,ok:"关闭",okCallback: returnlist});

        }else if(result==2)
        {
          $.dialog({type: "error", content: "该目录下有商品信息，不能删除！", modal: true, autoCloseTime: 3000});
        }else if(result==3)
        {
           $.dialog({type: "error", content: "该目录下有子目录，不能删除！", modal: true, autoCloseTime: 3000});
        }
	   
	   }); 
	  }
  }
  
  function returnlist()
  {
    location.href='typelist.do';  
  }
 --> 
</script>
</html>